package controller;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.session.Session;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import entity.Comment;
import entity.CommentResult;
import entity.HomeNews;
import entity.Message;
import entity.Note;
import entity.School;
import entity.User;
import net.sf.json.JSONObject;
import service.CommentService;
import service.HomeNewsService;
import service.MessageService;
import service.NoteService;
import service.NoteTypeService;
import service.SchoolService;
import service.UserService;
import util.ResponseUtil;
import util.ResultFormatUtil;

@Controller
@RequestMapping("/community")
public class CommunityController {
	@Resource
	private NoteService noteService;
	@Resource
	private MessageService messageService;
	@Resource
	private NoteTypeService noteTypeService;
	@Resource
	private SchoolService schoolService;
	@Resource
	private UserService userService;
	@Resource
	private CommentService commentService;
	//http://localhost:8080/CampusApp/community/getCommentData.do?noteId=1
	@RequestMapping("/getCommentData")
	public void getCommentData(HttpServletRequest request, HttpServletResponse response) throws Exception {
		String note_Id = request.getParameter("noteId");
		int noteId = Integer.parseInt(note_Id);
		if(noteId>0){
		List<Comment> commentList=commentService.getCommentListByNoteId(noteId);
		if(commentList!=null){
			List<CommentResult> commentResults=new ArrayList<>();
			for(int i=0;i<commentList.size();i++){
				CommentResult commentResult = new CommentResult();
				Comment comment = commentList.get(i);
				commentResult.setComment(comment);
				User user = userService.getUserByUserId(comment.getEditorId());
				commentResult.setEditorName(user.getUserName());
				//commentResult.setEditor(user);
				School school = schoolService.getSchoolBySchoolId(user.getSchooleId());
				commentResult.setSchool(school);
				Note note=noteService.getNoteByNoteId(comment.getNoteId());
				commentResult.setNote(note);
				commentResults.add(commentResult);
			}
			JSONObject result = ResultFormatUtil.getResultFrom(true, "commentList获取成功", commentResults);
			ResponseUtil.write(response, result);
		}else{
			JSONObject result = ResultFormatUtil.getResultFrom(false, "commentList获取失败", null);
			ResponseUtil.write(response, result);
		}
		}else{
			JSONObject result = ResultFormatUtil.getResultFrom(false, "noteId转型失败", null);
			ResponseUtil.write(response, result);
		}
		
	}
	//http://localhost:8080/CampusApp/community/publishComment.do?noteId=1&content=newNote&editor=1
		@RequestMapping("/publishComment")
		public void publishComment(HttpServletRequest request, HttpServletResponse response) throws Exception {
			String note_Id=request.getParameter("noteId");
			int noteId=Integer.parseInt(note_Id);
			String content=request.getParameter("content");
			String editor_Id=request.getParameter("editorId");
			int editorId=Integer.parseInt(editor_Id);
			Comment newComment = new Comment();
			newComment.setNoteId(noteId);
			newComment.setContent(content);
			newComment.setEditorId(editorId);
			//
			int flag=commentService.publishNewComment(newComment);
			if(flag>0){
				Message message = new Message();
				message.setActionId(1);
				List<Comment> comments=commentService.selectAllComment();
				message.setCommentId(comments.get(comments.size()-1).getCommentId());
				message.setNoteId(noteId);
				int count=messageService.addNewMessage(message);
				JSONObject result = ResultFormatUtil.getResultFrom(true, "新评论发布成功", null);
				ResponseUtil.write(response, result);
			}else{
				JSONObject result = ResultFormatUtil.getResultFrom(false, "新评论发布失败", null);
				ResponseUtil.write(response, result);
			}
		}
		
	
//	@Resource
//	private HomeNewsService homeNewsService;
//	//http://localhost:8080/CampusApp/homeNews/getHomeNewData.do
//	@RequestMapping("/getHomeNewData")
//	public void getHomeNewData(HttpServletRequest request, HttpServletResponse response) throws Exception {
//		List<HomeNews> allHomeNews = homeNewsService.getAllHomeNews();
//		if (allHomeNews!=null) {
//			JSONObject result = ResultFormatUtil.getResultFrom(true, "HomeNews集合获取成功", allHomeNews);
//			ResponseUtil.write(response, result);
//		}else{
//			JSONObject result = ResultFormatUtil.getResultFrom(false, "HomeNews集合获取失败", allHomeNews);
//			ResponseUtil.write(response, result);
//		}
//		
//
//	}

		//http://192.168.16.140:8080/CampusApp/user/adduser.do
	
	
	
}
